package com.wangjiangwen.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.wangjiangwen.bean.R;
import com.wangjiangwen.bean.user.CaptchaVO;
import com.wangjiangwen.bean.user.ChangePasswordDTO;
import com.wangjiangwen.bean.user.LoginDTO;
import com.wangjiangwen.bean.user.UserVO;
import com.wangjiangwen.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gavin
 * @since 2024-07-01
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @GetMapping("/captcha")
    public R<CaptchaVO> getCaptcha(@RequestParam("type") String type) {
        CaptchaVO captchaVO = userService.getCaptCha(type);
        return R.ok(captchaVO);
    }

    @PostMapping("/login")
    public R<SaTokenInfo> login(@RequestBody LoginDTO loginDTO) {
        SaTokenInfo tokenInfo = userService.login(loginDTO);
        return R.ok(tokenInfo);
    }

    @GetMapping("/logout")
    public R<Void> logout() {
        userService.logout();
        return R.ok();
    }

    @GetMapping("/getInfo")
    public R<UserVO> getInfo() {
        UserVO userVO = userService.getInfo();
        return R.ok(userVO);
    }

    @PostMapping("changePassword")
    public R<Void> changePassword(@RequestBody @Validated ChangePasswordDTO changePasswordDTO) {
        userService.changePassword(changePasswordDTO);
        return R.ok("修改密码成功");
    }
}
